package com.anxin.registerManagement.service;

import cn.hutool.core.util.ObjectUtil;
import com.anxin.registerManagement.bean.dto.PostBarDto;
import com.anxin.registerManagement.bean.jo.PostBarSearchJo;
import com.anxin.registerManagement.bean.persist.PostBar;
import com.anxin.registerManagement.bean.persist.User;
import com.anxin.registerManagement.mapper.PostBarMapper;
import com.anxin.registerManagement.mapper.UserMapper;
import com.anxin.registerManagement.util.ResultUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 叶前呈
 * @date 2022/2/20 14:30
 */
@Service
public class PostBarService {

    private static Logger logger = LoggerFactory.getLogger(PostBarService.class);

    @Autowired
    PostBarMapper postBarMapper;

    @Autowired
    UserMapper userMapper;

    /**
     * 查询贴吧
     */
    public PageInfo<PostBarDto> getPostBarList(PostBarSearchJo postBarSearchJo) {
        logger.info("查询贴吧");
        if (!ObjectUtil.isEmpty(postBarSearchJo.getPageNum()) && !ObjectUtil.isEmpty(postBarSearchJo.getPageSize())) {
            PageHelper.startPage(postBarSearchJo.getPageNum(), postBarSearchJo.getPageSize());
        }
        QueryWrapper<PostBar> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", postBarSearchJo.getTitle()).orderByDesc("is_top", "id");
        List<PostBar> postBarList = postBarMapper.selectList(queryWrapper);
        // 设置楼主名称
        List<PostBarDto> postBarDtoList = new ArrayList<>();
        for (PostBar postBar : postBarList) {
            User user = userMapper.selectById(postBar.getLandlordId());
            PostBarDto postBarDto = new PostBarDto();
            BeanUtils.copyProperties(postBar, postBarDto);
            postBarDto.setLandlordName(user.getNickName());
            postBarDtoList.add(postBarDto);
        }
        return new PageInfo<>(postBarDtoList);
    }

    /**
     * 发帖
     */
    public void addPostBar(PostBar postBar) {
        // TODO:这里需要从jwt里面获取当前操作人id
        postBar.setLandlordId(1);
        postBar.setLikeCount(0);
        postBar.setPageViewCount(0);
        postBar.setCommentCount(0);
        if (!ObjectUtil.isEmpty(postBar.getIsTop())) {
            postBar.setIsTop(postBar.getIsTop());
        }
        postBar.setIsTop(false);
        postBarMapper.insert(postBar);
    }

    /**
     * 查看帖子
     */
    public PostBar getPostBarById(Integer id) {
        //这里更新浏览量
        PostBar postBar = postBarMapper.selectById(id);
        postBar.setPageViewCount(postBar.getPageViewCount() + 1);
        postBarMapper.updateById(postBar);
        // TODO:这里应该封装dto，同时返回评论和回复信息
        return postBarMapper.selectById(id);
    }

    /**
     * 删除帖子
     */
    public void deletePostBar(List<Integer> id) {
        postBarMapper.deleteBatchIds(id);
        // FIXME:这里还需要删除对应的回复、评论等
    }

    /**
     * 点赞
     */
    public void clickLike(Integer id) {
        PostBar postBar = postBarMapper.selectById(id);
        postBar.setLikeCount(postBar.getLikeCount() + 1);
        postBarMapper.updateById(postBar);
    }

}
